function varargout = ShearViewGUI(varargin)
% SHEARVIEWGUI M-file for ShearViewGUI.fig
%      SHEARVIEWGUI, by itself, creates a new SHEARVIEWGUI or raises the existing
%      singleton*.
%
%      H = SHEARVIEWGUI returns the handle to a new SHEARVIEWGUI or the handle to
%      the existing singleton*.
%
%      SHEARVIEWGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHEARVIEWGUI.M with the given input arguments.
%
%      SHEARVIEWGUI('Property','Value',...) creates a new SHEARVIEWGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ShearViewGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ShearViewGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ShearViewGUI

% Last Modified by GUIDE v2.5 25-Aug-2008 12:34:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ShearViewGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @ShearViewGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ShearViewGUI is made visible.
function ShearViewGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ShearViewGUI (see VARARGIN)

% Choose default command line output for ShearViewGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% Populate the listboxes
load('D:\research\matlabCode\matlab_shearView\shearView_mcode\SV_defaults.mat','defaultpath');
initial_dir = defaultpath;
if ~isdir(initial_dir)
    initial_dir = pwd;
end
load_INTlistbox(initial_dir, handles)
load_PClistbox(initial_dir, handles)
load_ROIlistbox(initial_dir, handles)
set(handles.uipanel_PolarInterp ,'UserData','*linear');


% Return figure handle as first output argument

% UIWAIT makes ShearViewGUI wait for user response (see UIRESUME)
% uiwait(handles.mainGUI);


% --- Outputs from this function are returned to the command line.
function varargout = ShearViewGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in listbox_INTfolders.
function listbox_INTfolders_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_INTfolders (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_INTfolders contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_INTfolders

% this gets the current path that the intesity listbox is in
file_struct = get(handles.listbox_INTfolders,'Userdata');
currentpath = file_struct.currentpath;
cd(currentpath)

if  strcmp(get(handles.mainGUI,'SelectionType'),'open') 
    index_selected = get(handles.listbox_INTfolders,'Value');
    file_list = get(handles.listbox_INTfolders,'String');
    filename = file_list{index_selected};
    if strcmp('..',filename)
        cd(filename)
        load_INTlistbox(pwd,handles)
    end
elseif  strcmp(get(handles.mainGUI,'SelectionType'),'normal')
    index_selected = get(handles.listbox_INTfolders,'Value');
    file_list = get(handles.listbox_INTfolders,'String');
    filename = file_list{index_selected};
    if isdir(filename) && ~strcmp('..',filename)
        cd(filename)
        load_INTlistbox(pwd,handles)
    elseif ~strcmp('..',filename)
        display_dcm('axesINT',filename,handles)
        set(handles.axesINT,'UserData',filename)
        gray_ROItext = [handles.uipanel_ROI,... % assigns the handles of the 
                        handles.text_ROI1,...   % grayed out items in the ROI
                        handles.text_ROI2,...   % panel
                        handles.text_ROI3,...
                        handles.text_ROI4,...
                        handles.edit_OutputPath];                  
        set(gray_ROItext,'ForegroundColor','k')
        set(handles.listbox_ROI,'Enable','on')
        cd('..') % do this for the next command
        set(handles.edit_OutputPath,'String',pwd)
    end
end

% --- Executes during object creation, after setting all properties.
function listbox_INTfolders_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_INTfolders (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox_PCfolders.
function listbox_PCfolders_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_PCfolders (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_PCfolders contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_PCfolders

file_struct = get(handles.listbox_PCfolders,'Userdata');
currentpath = file_struct.currentpath;
cd(currentpath)

if  strcmp(get(handles.mainGUI,'SelectionType'),'open') 
    index_selected = get(handles.listbox_PCfolders,'Value');
    file_list = get(handles.listbox_PCfolders,'String');
    filename = file_list{index_selected};

    if strcmp('..',filename)
        cd(filename)
        load_PClistbox(pwd,handles)
    end
elseif  strcmp(get(handles.mainGUI,'SelectionType'),'normal')
    index_selected = get(handles.listbox_PCfolders,'Value');
    file_list = get(handles.listbox_PCfolders,'String');
    filename = file_list{index_selected};
    if isdir(filename) && ~strcmp('..',filename)
        cd(filename)
        load_PClistbox(pwd,handles)
    elseif ~strcmp('..',filename)
        display_dcm('axesPC',filename,handles)
        set(handles.axesPC,'UserData',filename)
    end
end


% --- Executes during object creation, after setting all properties.
function listbox_PCfolders_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_PCfolders (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in listbox_ROI.
function listbox_ROI_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_ROI contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_ROI

file_struct = get(handles.listbox_ROI,'Userdata');
currentpath = file_struct.currentpath;
cd(currentpath)

if  strcmp(get(handles.mainGUI,'SelectionType'),'open') 
    index_selected = get(handles.listbox_ROI,'Value');
    file_list = get(handles.listbox_ROI,'String');
    filename = file_list{index_selected};
    if strcmp('..',filename)
        cd(filename)
        load_ROIlistbox(pwd,handles)
        set(handles.pushbutton_ROIpreview,'Enable','off')
    end
elseif  strcmp(get(handles.mainGUI,'SelectionType'),'normal')
    index_selected = get(handles.listbox_ROI,'Value');
    file_list = get(handles.listbox_ROI,'String');
    filename = file_list{index_selected};
    if isdir(filename) && ~strcmp('..',filename)
        set(handles.pushbutton_ROIpreview,'Enable','off')
        cd(filename)
        load_ROIlistbox(pwd,handles)
    elseif ~strcmp('..',filename)
        set(handles.pushbutton_ROIpreview,'Enable','on',...
            'ForegroundColor','k',...
            'UserData',[currentpath '\' filename])
    end
end


% --- Executes during object creation, after setting all properties.
function listbox_ROI_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in pushbutton_runWSS.
function pushbutton_runWSS_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_runWSS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

file_struct = get(handles.listbox_INTfolders,'Userdata');
SV_parameters.Intdir    = file_struct.currentpath;

file_struct = get(handles.listbox_PCfolders,'Userdata');
SV_parameters.Veldir    = file_struct.currentpath;

filename = get(handles.pushbutton_ROIpreview,'UserData');
[pathstr name ext] = fileparts(filename);
SV_parameters.ROIdir = pathstr;

SV_parameters.Intfile   = get(handles.axesINT,'UserData');  
SV_parameters.Velfile   = get(handles.axesPC,'UserData');
SV_parameters.ROIfile   = [name ext];

%get switches for save figures, movies, and working directory, workspace
%name

SV_parameters.wkgdir      = get(handles.edit_OutputPath     ,'String');
SV_parameters.wkspaceName = get(handles.edit_OutputName     ,'String');
SV_parameters.saveVelFlow = get(handles.checkbox_VelFlowProf,'Value');
SV_parameters.movVelFlow  = get(handles.checkbox_movVelFlow ,'Value');
SV_parameters.includeDB   = get(handles.checkbox_IncludeDB  ,'Value');
SV_parameters.patientSum  = get(handles.checkbox_WSSsummary ,'Value');
SV_parameters.interp      = get(handles.uipanel_PolarInterp ,'UserData');
SV_parameters.PatientID   = get(handles.edit_PatientID      ,'String');
SV_parameters.Systole     = get(handles.checkbox_Systole    ,'Value');
SV_parameters.Platform    = get(handles.popupmenu_Manufacturer, 'Value');

if SV_parameters.includeDB && get(handles.radiobutton_BAV,'Value')
    SV_parameters.DBname  = 'BAV';
elseif SV_parameters.includeDB && get(handles.radiobutton_PHT,'Value')
    SV_parameters.DBname  = 'PHT';
    if get(handles.radiobutton_MPA,'Value')
        SV_parameters.DBsubtype  = 'MPA';
    elseif get(handles.radiobutton_LPA,'Value')
        SV_parameters.DBsubtype  = 'LPA';
    elseif get(handles.radiobutton_RPA,'Value')
        SV_parameters.DBsubtype  = 'RPA';
    else
        errordlg('Choose a patient DB!','Need more info');
        return
    end
elseif SV_parameters.includeDB && get(handles.radiobutton_EX,'Value')
    SV_parameters.DBname  = 'Phantom';    
elseif SV_parameters.includeDB
    errordlg('Choose a patient DB!','Need more info');
    return
end

h_velFlowProf = ShearView(SV_parameters);
if SV_parameters.patientSum
    h_WSSsummary  = plotWSSsummary(SV_parameters);
end

button = questdlg('Do you want to close the output windows and continue?');
if strcmp(button,'Yes') && SV_parameters.patientSum
    close([h_velFlowProf h_WSSsummary])
elseif strcmp(button,'Yes') && ~SV_parameters.patientSum
    close(h_velFlowProf)
end

% --- Executes on selection change in popupmenu_OutputName.
function popupmenu_OutputName_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_OutputName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_OutputName contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_OutputName

% Determine the selected data set.
str = get(hObject, 'String');
val = get(hObject,'Value');
% Set current data to the selected data set.
if ~strcmp(str{val},'Choose Name') && ~strcmp(str{val},'[Enter Manually]')
    set(handles.edit_OutputName,'String',str{val},'ForegroundColor','k');
elseif strcmp(str{val},'[Enter Manually]') 
    prompt = {'Enter Name of Output File'};
    dlg_title = 'Output File';
    num_lines = 1;
    def = {'DefaultName.mat'};
    outputfile = inputdlg(prompt,dlg_title,num_lines,def);
    set(handles.edit_OutputName,'String',outputfile{:},'ForegroundColor','k');
elseif strcmp(str{val},'[Use Patient DB]')
    set(handles.edit_OutputName,'String',str{val});
end

set(handles.pushbutton_runWSS,'Enable','on')

% --- Executes during object creation, after setting all properties.
function popupmenu_OutputName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_OutputName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over listbox_INTfolders.
function listbox_INTfolders_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to listbox_INTfolders (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton_ROIpreview.
function pushbutton_ROIpreview_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ROIpreview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

filename = get(handles.pushbutton_ROIpreview,'UserData');
preview_ROI(filename,handles)

% Populate the Intensity Listbox
% -------------------------------------------------------------------------
function load_INTlistbox(dir_path, handles)

cd (dir_path)

dir_struct                  = dir(dir_path);
temp                        = struct2cell(dir_struct);
tempname                    = temp(1,:);
tempisdir                   = temp(4,:);
filtered_folders            = tempname(cell2mat(tempisdir));
ind                         = strfind(tempname,'.dcm');
tf                          = cellfun(@isscalar,ind);
filtered_items              = [filtered_folders tempname(tf)];
[sorted_names,sorted_index] = sortrows(filtered_items');
filedata.currentpath        = dir_path;

set(handles.listbox_INTfolders,'String',sorted_names,...
	'Value',1,'Userdata',filedata)
set(handles.text_INTfolder2,'String',pwd)

% Populate the PhaseContrast Listbox
% -------------------------------------------------------------------------
function load_PClistbox(dir_path, handles)

cd (dir_path)

dir_struct                  = dir(dir_path);
temp                        = struct2cell(dir_struct);
tempname                    = temp(1,:);
tempisdir                   = temp(4,:);
filtered_folders            = tempname(cell2mat(tempisdir));
ind                         = strfind(tempname,'.dcm');
tf                          = cellfun(@isscalar,ind);
filtered_items              = [filtered_folders tempname(tf)];
[sorted_names,sorted_index] = sortrows(filtered_items');
filedata.currentpath        = dir_path;

set(handles.listbox_PCfolders,'String',sorted_names,...
	'Value',1,'Userdata',filedata)
set(handles.text_PCfolder2,'String',pwd)

% Populate the ROI Listbox
% -------------------------------------------------------------------------
function load_ROIlistbox(dir_path, handles)

cd (dir_path)

dir_struct                  = dir(dir_path);
temp                        = struct2cell(dir_struct);
tempname                    = temp(1,:);
tempisdir                   = temp(4,:);
filtered_folders            = tempname(cell2mat(tempisdir));
ind                         = regexp(tempname,'.(txt)|(seg)'); 
tf                          = cellfun(@isscalar,ind);
filtered_items              = [filtered_folders tempname(tf)];
[sorted_names,sorted_index] = sortrows(filtered_items');
filedata.currentpath        = dir_path;

set(handles.listbox_ROI,'String',sorted_names,...
	'Value',1,'Userdata',filedata)

% preview dicom file
% -------------------------------------------------------------------------
function display_dcm(axes2update,filename,handles)
I = dicomread(filename);
axes(eval(['handles.' axes2update]))
himage = imagesc(I);
colormap(gray)
axis equal
set(gca,'XTick',[],'YTick',[])

% now write the filename of the .dcm to the image handle for future
% reference (esp. to pull the timestep for the previewROI funcion)
set(himage,'UserData',filename);

% preview ROI and cropped intensity image
% -------------------------------------------------------------------------
function preview_ROI(filename,handles)

%change mouse pointer to hourglass
set(handles.mainGUI,'Pointer','watch')
drawnow
%get path of INT directory
file_struct = get(handles.listbox_INTfolders,'Userdata');
currentpath = file_struct.currentpath;
cd(currentpath)
file = dir;
basefilenumber = file(3).name;
basefilenumber = basefilenumber(9:12);
basefilenumber = str2double(basefilenumber);

% find Intensity filename & cdata, then find timestep of intensity image
himage       = get(handles.axesINT,'children');
INT_filename = get(himage,'UserData');
INT_Cdata    = get(himage,'Cdata');

tstep = INT_filename(9:12);    % gets string of tstep
tstep = str2double(tstep);     % converts string to number

% get number of lowest file # in directory then subtract it-1 from tstep
tstep = tstep - basefilenumber + 1;


% obtain all ROI information depending on ROI filetype and store locally
[pathstr name ext] = fileparts(filename);

if strcmp(ext,'.txt')
    ROI = getROI_ARGUS(filename);
elseif strcmp(ext,'.seg')
    [ROI_X ROI_Y] = getSegmentROI(filename);
    for i = 1:size(ROI_X,2)
        ROI(:,:,i) = [ROI_Y(:,i),ROI_X(:,i)];
    end
end
userstruct.filename = filename;

%plot up information
axes(handles.axesROI);
Xmin = min(nonzeros(ROI(:,1,:)))-5; % 5 pixels zoom out...
Xmax = max(nonzeros(ROI(:,1,:)))+5; % TODO: need to crop image according to this
Ymin = min(nonzeros(ROI(:,2,:)))-5;
Ymax = max(nonzeros(ROI(:,2,:)))+5;
INT_Cdata = imcrop(INT_Cdata,[Xmin Ymin Xmax-Xmin Ymax-Ymin]);
imagesc(INT_Cdata);
axis ij; axis equal; hold on; axis manual
set(gca,'XTick',[],'YTick',[])
plot(nonzeros(ROI(:,1,tstep,1))-Xmin+1,nonzeros(ROI(:,2,tstep,1))-Ymin+1,'g','LineWidth',2)
if size(ROI,4) > 1
    plot(nonzeros(ROI(:,1,tstep,2))-Xmin+1,nonzeros(ROI(:,2,tstep,2))-Ymin+1,'r','LineWidth',2)
end
hold off
set(handles.mainGUI,'Pointer','arrow')

% --------------------------------------------------------------------
function uimenu_about_Callback(hObject, eventdata, handles)
% hObject    handle to uimenu_about (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

 msgbox('This program was created by Alex Barker (barkeraj@colorado.edu) as a part of his doctoral thesis. Please reference this program if you publish any work using this tool.  Thanks!')






function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_OutputName_Callback(hObject, eventdata, handles)
% hObject    handle to edit_OutputName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_OutputName as text
%        str2double(get(hObject,'String')) returns contents of edit_OutputName as a double


% --- Executes during object creation, after setting all properties.
function edit_OutputName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_OutputName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton_OutputPath.
function pushbutton_OutputPath_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_OutputPath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

startpath = get(handles.edit_OutputPath,'String');
newpath = uigetdir(startpath);
if ischar(newpath) 
    set(handles.edit_OutputPath,'String',newpath)
end

% --- Executes during object creation, after setting all properties.
function edit_OutputPath_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_OutputPath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in radiobutton_Nearest.
function radiobutton_Nearest_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_Nearest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_Nearest

set(handles.uipanel_PolarInterp,'UserData','*nearest')

% --- Executes on button press in radiobutton_bilinear.
function radiobutton_bilinear_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_bilinear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_bilinear

set(handles.uipanel_PolarInterp,'UserData','*linear')

% --- Executes on button press in radiobutton_spline.
function radiobutton_spline_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_spline (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_spline

set(handles.uipanel_PolarInterp,'UserData','*cubic')

% --- Executes on button press in radiobutton_bicubic.
function radiobutton_bicubic_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_bicubic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_bicubic

% TODO: Create Default path preferences and save
% TODO: Create button to save current paths as defaults

set(handles.uipanel_PolarInterp,'UserData','*cubic')

% --------------------------------------------------------------------
function uimenu_DefaultPaths_Callback(hObject, eventdata, handles)
% hObject    handle to uimenu_DefaultPaths (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

defaultpath = uigetdir(pwd,'pick a new default start path');
if defaultpath == 0
    defaultpath = pwd
end
save('C:\Documents and Settings\Alex\My Documents\MATLAB\ShearView\SV_defaults.mat','defaultpath')
ShearViewGUI_OpeningFcn(hObject, eventdata, handles)



% --- Executes on button press in checkbox_IncludeDB.
function checkbox_IncludeDB_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_IncludeDB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_IncludeDB

grayedout = [handles.uipanel_whichone,...
             handles.radiobutton_BAV,...
             handles.radiobutton_PHT,...
             handles.radiobutton_EX];
val       =  get(hObject,'Value');
if val == 1
    set(grayedout,'ForegroundColor','k')
    set(grayedout(2:4),'Enable','on')
else
    set(grayedout(2:4),'Enable','off')
    set(grayedout(1),'ForegroundColor',[.5 .5 .5])
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
end


% --- Executes on button press in radiobutton_BAV.
function radiobutton_BAV_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_BAV (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_BAV

grayedout = [handles.uipanel_location,...
             handles.radiobutton_MPA,...
             handles.radiobutton_LPA,...
             handles.radiobutton_RPA];
val       =  get(hObject,'Value');
if val == 1
    set(grayedout(2:4),'Enable','off')
    set(grayedout(1),'ForegroundColor',[.5 .5 .5])
    set(handles.edit_PatientID,'Enable','on','ForegroundColor','k')
elseif val == 0
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
end

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');
if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]')
    set(handles.edit_OutputName,'String','AAo.mat')
end

% --- Executes on button press in radiobutton_PHT.
function radiobutton_PHT_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_PHT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_PHT

grayedout = [handles.uipanel_location,...
             handles.radiobutton_MPA,...
             handles.radiobutton_LPA,...
             handles.radiobutton_RPA];
val       =  get(hObject,'Value');
if val == 1
    set(grayedout,'ForegroundColor','k')
    set(grayedout(2:4),'Enable','on')
elseif val == 0
    set(grayedout(2:4),'Enable','off')
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
end

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');
if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]') 
    set(handles.edit_OutputName,'String','DB Location needed')
end



% --- Executes during object creation, after setting all properties.
function pushbutton_ROIpreview_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_ROIpreview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called




% --- Executes on button press in checkbox_movVelFlow.
function checkbox_movVelFlow_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_movVelFlow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_movVelFlow


% --- Executes on button press in checkbox10.
function checkbox10_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox10


% --- Executes on button press in radiobutton_MPA.
function radiobutton_MPA_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_MPA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_MPA

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');

if get(hObject,'Value') == 1
    set(handles.edit_PatientID,'Enable','on','ForegroundColor','k')
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]') || strcmp(UseDB4wkspaceName,'DB Location needed')
        set(handles.edit_OutputName,'String','MPA.mat')
    end
else
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]') 
        set(handles.edit_OutputName,'String','DB Location needed')
    end
end

% --- Executes on button press in radiobutton_LPA.
function radiobutton_LPA_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_LPA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_LPA

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');

if get(hObject,'Value') == 1
    set(handles.edit_PatientID,'Enable','on','ForegroundColor','k')
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]') || strcmp(UseDB4wkspaceName,'DB Location needed')
        set(handles.edit_OutputName,'String','LPA.mat')
    end
else
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]')
        set(handles.edit_OutputName,'String','DB Location needed')
    end
end

% --- Executes on button press in radiobutton_RPA.
function radiobutton_RPA_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_RPA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_RPA

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');

if get(hObject,'Value') == 1
    set(handles.edit_PatientID,'Enable','on','ForegroundColor','k')
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]') || strcmp(UseDB4wkspaceName,'DB Location needed')
        set(handles.edit_OutputName,'String','RPA.mat')
    end
else
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
    if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]')
        set(handles.edit_OutputName,'String','DB Location needed')
    end
end

% --- Executes on selection change in popupmenu_patientID.
function popupmenu_patientID_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_patientID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_patientID contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_patientID


% --- Executes during object creation, after setting all properties.
function popupmenu_patientID_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_patientID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_PatientID_Callback(hObject, eventdata, handles)
% hObject    handle to edit_PatientID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_PatientID as text
%        str2double(get(hObject,'String')) returns contents of edit_PatientID as a double


% --- Executes during object creation, after setting all properties.
function edit_PatientID_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_PatientID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in checkbox_positive.
function checkbox_positive_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_positive (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_positive


% --- Executes on button press in checkbox_Systole.
function checkbox_Systole_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_Systole (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_Systole




% --- Executes on key press with focus on edit_OutputName and no controls selected.
function edit_OutputName_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to edit_OutputName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function edit_OutputPath_Callback(hObject, eventdata, handles)
% hObject    handle to edit_OutputPath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_OutputPath as text
%        str2double(get(hObject,'String')) returns contents of edit_OutputPath as a double


% --- Executes on button press in radiobutton_EX.
function radiobutton_EX_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_EX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton_EX

grayedout = [handles.uipanel_location,...
             handles.radiobutton_MPA,...
             handles.radiobutton_LPA,...
             handles.radiobutton_RPA];
val       =  get(hObject,'Value');
if val == 1
    set(grayedout(2:4),'Enable','off')
    set(grayedout(1),'ForegroundColor',[.5 .5 .5])
    roifilename = get(handles.pushbutton_ROIpreview,'UserData');
    [p, expname, e, v] = fileparts(roifilename);
    clear p e v roifilename
    set(handles.edit_PatientID,'Enable','on','ForegroundColor','k','String',expname)
elseif val == 0
    set(handles.edit_PatientID,'Enable','off','ForegroundColor',[.5 .5 .5])
end

UseDB4wkspaceName = get(handles.popupmenu_OutputName, 'String');
val               = get(handles.popupmenu_OutputName, 'Value');
if strcmp(UseDB4wkspaceName{val},'[Use Patient DB]')
    set(handles.edit_OutputName,'String',[expname '.mat'])
end


% --- Executes during object creation, after setting all properties.
function axesINT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axesINT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axesINT




% --- Executes on selection change in popupmenu_Manufacturer.
function popupmenu_Manufacturer_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_Manufacturer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_Manufacturer contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_Manufacturer


% --- Executes during object creation, after setting all properties.
function popupmenu_Manufacturer_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_Manufacturer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


